/*
 * Created on Jul 21, 2004
 */
package com.scalar.chiptrack.operations.packingslip.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.commons.ClientInfo;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.packingslip.CustomerShippingLineItems;
import com.scalar.chiptrack.operations.packingslip.dao.NewPackingSlipDAO;
import com.scalar.chiptrack.operations.packingslip.forms.PackingSlipForm;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.sales.dao.SalesDAO;
import com.scalar.chiptrack.users.dao.UserDAO;

/**
 * @author Ravikanth
**/

public class PrintPackingSlipAction
{

	private static String ERROR_MESSAGE_CONTACT_SYS_ADMN = "Please.. Contact <a href='javascript:showErrorReportPage()'>System Administrator</a>";

	public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception 
	{
		HttpSession session = request.getSession();
		HashMap errors		= new HashMap();
		String returnVal 	= "failure";
		String action 		= request.getParameter( "action" );
		boolean dropShip	= false;
		int customerShippingId  = -1 ;
 
		try
		{ 
			customerShippingId = Integer.parseInt ( ( String ) request.getParameter ( "customerShippingId" ) ); 
		} 
		catch (NumberFormatException nfe) {}
		
		if ( ( customerShippingId != -1 ) )
		{
			boolean success = false;
			PackingSlipForm packingSlipForm = null;
			
			try
			{
				packingSlipForm = NewPackingSlipDAO.loadPackingSlipById ( customerShippingId );
				packingSlipForm.setAction( action );
				String vendorLocationName = VendorDAO.getLocationByAddressId( packingSlipForm.getShippingFromLocationId(), Constants.LOCATION_TYPE_FINISHED_GOODS );
				request.setAttribute( "vendorLocationName", vendorLocationName );
				success = true;
			}
			catch ( ScalarException se )
			{
				errors.put ("Internal Server Error", "Could not load Packing Slip");
				errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
				request.setAttribute( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not load Packing Slip" );
				session.setAttribute( SessionKeys.ERROR_REPORT, se );
				request.setAttribute(SessionKeys.ERROR_MESSAGES, errors);
			}

			if ( success == true )
			{
				if ( "labelView".equalsIgnoreCase ( action) )
				{
					 packingSlipForm.setFinalInventoryDataMap( NewPackingSlipDAO.loadLotNumberDataByCustomerShippingId( packingSlipForm.getCustomerShippingId() ) );
					 request.setAttribute( "loadInnerOuterBoxById" , NewPackingSlipDAO.loadInnerOuterBoxById( packingSlipForm ) );
					 returnVal = "viewLabels";
				}
				if( "printView".equalsIgnoreCase ( action ) )
				{
                    String recipientType = request.getParameter("recipientType");
                    request.setAttribute( "recipientType" , request.getParameter("recipientType") );
                    //here we setting dropshipment=false when customer open's ps and dropshipment=true when vendor open's ps
                    //for getting dropshipment printer friendly for vendor and ps printer friendly for customer
                    if( "customer".equalsIgnoreCase(recipientType) && ! Constants.CUSTOMER_SHIPPING_STATUS_CANCELLED.equalsIgnoreCase( packingSlipForm.getStatus() ) )
                    {
                        packingSlipForm.setDropShippment( false );
                    }
                    else if ( ! Constants.CUSTOMER_SHIPPING_STATUS_CANCELLED.equalsIgnoreCase( packingSlipForm.getStatus() ) )
                    {
                        packingSlipForm.setDropShippment( true );
                    }
					//Added by varma to show the terms and conditions in PF versions
                    request.setAttribute ( "termsAndConditions", SalesDAO.loadTermsAndConditionsAsList () );
                    packingSlipForm.setFinalInventoryDataMap( NewPackingSlipDAO.loadLotNumberDataByCustomerShippingId( packingSlipForm.getCustomerShippingId() ) );
                    ClientInfo clientInfo = UserDAO.getClientAddressInfo(  );
					if(  Constants.CLIENT_NAME_AIRGO .equalsIgnoreCase( clientInfo.getClientName() ) )
                    {
                        ArrayList lineItemsList	= packingSlipForm.getLineitems();
                        for ( int i = 0; i < lineItemsList.size() ; i++ )
                        {
                            CustomerShippingLineItems cLineItem = null;
                            cLineItem = ( CustomerShippingLineItems ) lineItemsList.get(i);
                            cLineItem.setTotalOuterBoxes( NewPackingSlipDAO.loadOuterBoxesforLineItem(cLineItem)+"" );
                        }
                        returnVal = "packingSlipPrintViewAirgo";
                    }
                    else{
                         returnVal = "packingSlipPrintView";
                    }
				}
				request.setAttribute ( "packingSlipPrintForm", packingSlipForm );
			}
			else
			{
				errors.put ("Internal Server Error", "Could not load Packing Slip");
				errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
				request.setAttribute( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not load Packing Slip" );
				request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
				returnVal = "packingSlipMenu";
			}
		}
		else
		{
			errors.put ("Internal Server Error", "Could not load Packing Slip");
			errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
			request.setAttribute( SessionKeys.ERROR_REPORT_SUBJECT, "Error: Could not load Packing Slip" );
			request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
			returnVal = "packingSlipMenu";
		}
		return ( mapping.findForward ( returnVal ) );	
	}
}